package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensUserRoleRelation;
import com.ds.lens.data.service.dao.sql.provider.LensUserRoleRelationSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface LensUserRoleRelationDao {
    @Delete({
            "delete from lens_user_role_relation",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_user_role_relation (id, user_name, ",
            "role_id)",
            "values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, ",
            "#{roleId,jdbcType=BIGINT})"
    })
    int insert(LensUserRoleRelation record);

    @InsertProvider(type = LensUserRoleRelationSqlProvider.class, method = "insertSelective")
    int insertSelective(LensUserRoleRelation record);

    @Select({
            "select",
            "id, user_name, role_id",
            "from lens_user_role_relation",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "user_name", property = "userName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "role_id", property = "roleId", jdbcType = JdbcType.BIGINT)
    })
    LensUserRoleRelation selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensUserRoleRelationSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensUserRoleRelation record);

    @Update({
            "update lens_user_role_relation",
            "set user_name = #{userName,jdbcType=VARCHAR},",
            "role_id = #{roleId,jdbcType=BIGINT}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensUserRoleRelation record);


    @Select({"<script>",
            "select",
            "count(1) > 0 ",
            "from lens_user_role_relation",
            "where user_name = #{userName,jdbcType=VARCHAR} ",
            "<if test='roleId != null'> and role_id = #{roleId,jdbcType=BIGINT} </if>",
            "</script>"
    })
    boolean relationExisted(@Param("userName") String userName,
                            @Param("roleId") Long roleId);
}
